package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.user.RegisterParams;
import com.huizhonglm.diap.api.model.user.RegisterResponse;
import com.huizhonglm.diap.api.model.user.UserInfoParams;
import com.huizhonglm.diap.api.model.user.UserInfoResponse;
import com.huizhonglm.diap.core.storage.model.UserInfo;
import com.huizhonglm.diap.core.utils.DistrictUtil;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.model.District;
import com.huizhonglm.diap.ui.user.iView.RegisterIView;

/**
 * @author LiXiaoLong
 * @version 1.0
 * @since 2014-11-19 11:25
 */
public class RegisterPresenter {

    private RegisterIView mView;

    public RegisterPresenter(RegisterIView view) {
        mView = view;
    }

    public void register(final RegisterParams params) {
        API.http().post("register", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    RegisterResponse res = JSON.parseObject(result, RegisterResponse.class);
                    if (res != null && Status.SUCCESS.equals(res.getStatus())) {
                        switch (res.getStatus()) {
                            case Status.SUCCESS:
                                getUserInfo(res.getUserId());
                                break;
                            case Status.FAILURE:
                                mView.onRegisterFailure("注册失败，服务器异常");
                                break;
                            case Status.HAS_REG:
                                mView.onRegisterFailure("当前号码已被注册，请登录");
                                break;
                        }
                    } else {
                        mView.onRegisterFailure("注册失败，服务器异常");
                    }
                } else {
                    mView.onRegisterFailure("注册失败，服务器异常");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                mView.onRegisterFailure("注册失败，服务器异常");
            }
        });
    }


    private void getUserInfo(final String userId){
        UserInfoParams params = new UserInfoParams();
        params.userId = userId;
        API.http().post("getUserInfoByRegister", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    UserInfoResponse res = JSON.parseObject(result, UserInfoResponse.class);
                    if (res != null) {
                        District dist = DistrictUtil.getInstance().getDistrict(res.getZoneid());
                        UserInfo user = new UserInfo();
                        user.setUserId(userId);
                        user.setUserName(res.getName());
                        user.setNickName(res.getNickname());
                        user.setUserIcon(res.getHeaderImg());
                        user.setUserSex(res.getSex());
                        user.setProvinceId(res.getProvinceid());
                        user.setProvinceName(dist.getProvName());
                        user.setCityId(res.getCityid());
                        user.setCityName(dist.getCityName());
                        user.setDistrictId(res.getZoneid());
                        user.setDistrictName(dist.getDistName());
                        user.setAddress(res.getAddress());
                        user.setTwodCodeImg(res.getTwodCodeImg());
                        UserUtil.saveUserInfo(user);
                        mView.onRegisterSuccess();
                    } else {
                        mView.onRegisterFailure("当前号码已被注册，请登录");
                    }
                } else {
                    mView.onRegisterFailure("当前号码已被注册，请登录");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                mView.onRegisterFailure("当前号码已被注册，请登录");
            }
        });
    }
}
